package com.shishuo.cms.service;

import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Service;

import com.shishuo.cms.util.MailUtils;

@Service
public class MailService {
	private static Resource resource = new ClassPathResource("/mail.properties");
	private Properties props ;
	public boolean sendMail(String acceptEmail,String title,String content) throws Exception {
		if (null==acceptEmail||"".equals(acceptEmail)) {
			throw new Exception("accept mail address is not value");
		}
		props = PropertiesLoaderUtils.loadProperties(resource);
		MailUtils sendmail = new MailUtils();
		sendmail.setHost(props.getProperty("smtp"));
		sendmail.setUserName(props.getProperty("username"));
		sendmail.setPassWord(props.getProperty("password"));
		sendmail.setTo(acceptEmail);
		sendmail.setFrom(props.getProperty("username"));
		sendmail.setSubject(title);
		sendmail.setContent(content);
		//sendmail.attachfile("d:\\news.rar");
		//sendmail.attachfile("d:\\jhjl.rar");
		return sendmail.sendMail();
	}
	public boolean sendCallBackPasswordMail(String acceptEmail,String userName) throws Exception {
		props = PropertiesLoaderUtils.loadProperties(resource);
		String title= props.getProperty("callBackPasswordMailTitle");
		String content = getCallBackPasswordMailContent(props.getProperty("callBackPasswordMailContent"),userName);
		return sendMail(acceptEmail,title,content);
	}
	/**
	 * 获取要发送找回密码的邮件内容
	 * @param userEntity
	 * @return
	 */
	private String getCallBackPasswordMailContent(String content,String userName) {
		return content+userName;
	}
}
